<?php

namespace App\Http\Controllers\Web;

use App\Models\Customs;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CustomsController extends Controller
{
    public function index(Request $request)
    {
        $size = $request->input('size', 10);
        $query = Customs::query();
        $name= $request->input("name");
        $email = $request->input("email");
        $phone = $request->input("phone");
        $query->when($name, function ($query) use ($name) {
            $query->where('name', "like", "%{$name}%");
        });
        $query->when($email, function ($query) use ($email) {
            $query->where('email', "like", "%{$email}%");
        });
        $query->when($phone, function ($query) use ($phone) {
            $query->where('phone', "like", "%{$phone}%");
        });

        return $this->successPaginate($query->paginate($size));
    }

    public function requestData(Request $request)
    {
        $requestData = $this->validate($request, [
            'name' => 'required',
            'email' => 'required',
            'phone' => "required",
            'content' => 'nullable',
        ]);
        return $requestData;
    }

    public function show($id)
    {
        $custom = Customs::query()->findOrFail($id);
        return $this->success($new);
    }

    public function store(Request $request)
    {
        $custom = new Customs();
        $requestData = $this->requestData($request);
        $custom->fill($requestData);
        $custom->save();
        return $this->success($requestData);
    }

    public function update(Request $request, $id)
    {
        $custom = Customs::query()->findOrFail($id);
        $requestData = $this->requestData($request);
        $custom->fill($requestData);
        $custom->save();
        return $this->success($requestData);
    }

    public function destroy($id)
    {
        $custom = Customs::query()->findOrFail($id);
        $custom->delete();
    }
}
